#include"RenderableObject.h"
#include"Scene.h"
//#include"Camera.h"
#include"DirectionalLight.h"
#include"PointLight.h"

void RenderableObject::Draw()
{
	mesh.SetStream();
	mesh.SetVertexDeclaration();

	switch(shader){
		case Diffuse:{
			diffuseShader.effect->SetTechnique("Diffuse");
			diffuseShader.effect->SetMatrix("matWVP", &(mesh.LocalToWorldMatrix()*scene.mainCamera.ViewMatrix()*scene.mainCamera.ProjMatrix()));
			diffuseShader.effect->SetMatrix("matWorld", &(mesh.LocalToWorldMatrix()));
			diffuseShader.effect->SetRawValue("directionalLights", directionalLightsData, 0, sizeof(directionalLightsData));
			diffuseShader.effect->SetRawValue("pointLights", pointLightsData, 0, sizeof(pointLightsData));
			diffuseShader.effect->SetTexture("colorTex", material.colorTex);
			diffuseShader.effect->SetRawValue("mtlDiffuse", &(material.diffuse), 0, sizeof(D3DXCOLOR));

			diffuseShader.effect->Begin(0, 0);
			diffuseShader.effect->BeginPass(0);

			mesh.Draw();

			diffuseShader.effect->EndPass();
			diffuseShader.effect->End();

			break;
		}

		case Specular:{
			specShader.effect->SetTechnique("Specular");
			specShader.effect->SetMatrix("matWVP", &(mesh.LocalToWorldMatrix()*scene.mainCamera.ViewMatrix()*scene.mainCamera.ProjMatrix()));
			specShader.effect->SetMatrix("matWorld", &(mesh.LocalToWorldMatrix()));
			specShader.effect->SetRawValue("directionalLights", directionalLightsData, 0, sizeof(directionalLightsData));
			specShader.effect->SetRawValue("pointLights", pointLightsData, 0, sizeof(pointLightsData));
			specShader.effect->SetTexture("colorTex", material.colorTex);
			specShader.effect->SetRawValue("mtlDiffuse", &(material.diffuse), 0, sizeof(D3DXCOLOR));
			specShader.effect->SetRawValue("mtlSpec", &(material.shiness * material.spec), 0, sizeof(D3DXCOLOR));
			specShader.effect->SetFloat("gloss", material.gloss);
			specShader.effect->SetRawValue("eyePos", &(scene.mainCamera.position), 0, sizeof(Vector3));

			specShader.effect->Begin(0, 0);
			specShader.effect->BeginPass(0);

			mesh.Draw();

			specShader.effect->EndPass();
			specShader.effect->End();

			break;
		}
	}

	gizmo.rotation = mesh.rotation;
	if(showGizmo)
		gizmo.Draw(mesh.position);
	
}